﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_Entities;
using CoolERP_BLL;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class XSHT_Edit : BasePage
    {
        protected Guid id
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindFirst();
        }

        private void BindFirst()
        {
            BindDepartment(this.ddlDepartments, EnumDepartment.Marketing, "请选择");
            List<XSHTMXBModel> mxbs = new List<XSHTMXBModel>();
            if (id != Guid.Empty)
            {
                using (XSHTBLL xBLL = new XSHTBLL())
                {
                    liTitle.Text = "修改";
                    XSHT entity = xBLL.GetModel(t => t.ID == id);
                    hidKHID.Value = entity.KHID.ToString();
                    txtKH.Text = entity.ZL_KH.JIANCHENG;
                    if (!string.IsNullOrEmpty(entity.CHFS) && this.ddlCHFS.Items.FindByValue(entity.CHFS) != null)
                        this.ddlCHFS.SelectedValue = entity.CHFS;
                    if (entity.HTZT != null && this.ddlhtzt.Items.FindByValue(entity.HTZT.ToString()) != null)
                        this.ddlhtzt.SelectedValue = entity.HTZT.ToString();
                    if (entity.XSBMID != null && this.ddlDepartments.Items.FindByValue(entity.XSBMID.ToString()) != null)
                    {
                        this.ddlDepartments.SelectedValue = entity.XSBMID.ToString();
                        BindStaff(this.ddlDepartments.SelectedValue, this.ddlSaler, "请选择");
                        if (entity.XSRYID != null && this.ddlSaler.Items.FindByValue(entity.XSRYID.ToString()) != null)
                        {
                            this.ddlSaler.SelectedValue = entity.XSRYID.ToString();
                            hidStaff.Value = entity.XSRYID.ToString();
                        }
                    }
                    this.txtDHSJ.Text = entity.DHSJ == null ? "" : entity.DHSJ.Value.ToString("yyyy-MM-dd HH:mm");
                    this.txtCHSJ.Text = entity.CHSJ == null ? "" : entity.CHSJ.Value.ToString("yyyy-MM-dd HH:mm");

                }
                //明细表
                using (XSHTMXBBLL xBLL = new XSHTMXBBLL())
                {
                    int RecordCount = 0;
                    mxbs = xBLL.GetXSHTMXB(t => t.XSHTID == id, "", null, 1, out RecordCount);
                    repMXList.DataSource = mxbs;
                    repMXList.DataBind();
                    foreach (var mxb in mxbs)
                    {
                        hidCPID.Value += "," + mxb.CPID.ToString();
                        hidPrices.Value += "," + mxb.DJ.ToString();
                        hidCounts.Value += "," + mxb.SL.ToString();
                    }
                    hidCPID.Value = hidCPID.Value.Trim(',');
                    hidPrices.Value = hidPrices.Value.Trim(',');
                    hidCounts.Value = hidCounts.Value.Trim(',');
                }
            }
            if (mxbs.Count > 0)
            {
                spanMoney.Text = mxbs.Sum(t => t.SL * t.DJ).Value.ToString("F");
                divTotalMoney.Attributes["style"] = "text-align:right; margin:5px 0px; padding-right:130px";
            }
            else
                divTotalMoney.Attributes["style"] = "text-align:right; display:none; margin:5px 0px; padding-right:130px";
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            ZL_YWLSH ywItem = new ZL_YWLSH();
            bool isAdd = true;
            bool result = false;
            using (XSHTBLL xBLL = new XSHTBLL())
            {
                XSHT entity = new XSHT();
                if (id != Guid.Empty)
                    entity = xBLL.GetModel(t => t.ID == id);
                else
                {
                    entity.ID = Guid.NewGuid();
                    entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.XSHT), out ywItem, out isAdd);
                }
                entity.KHID = Guid.Parse(hidKHID.Value);
                entity.XSBMID = Guid.Parse(this.ddlDepartments.SelectedValue);
                entity.XSRYID = Guid.Parse(hidStaff.Value);
                entity.DHSJ = DateTime.Parse(this.txtDHSJ.Text.Trim());
                entity.CHSJ = DateTime.Parse(this.txtCHSJ.Text.Trim());
                entity.CHFS = this.ddlCHFS.SelectedValue;
                entity.HTZT = int.Parse(this.ddlhtzt.SelectedValue);
                //明细表
                List<XSHTMXB> delMXB = entity.XSHTMXB.ToList();//旧的待删明细表
                string[] ids = hidCPID.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string[] prices = hidPrices.Value.Split(new string[] { "," }, StringSplitOptions.None);
                string[] counts = hidCounts.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < ids.Count(); i++)
                {
                    XSHTMXB item = new XSHTMXB();
                    item.ID = Guid.NewGuid();
                    item.CPID = Guid.Parse(ids[i]);
                    item.SL = Convert.ToDouble(counts[i]);
                    item.DJ = Convert.ToDouble(prices[i]);
                    entity.XSHTMXB.Add(item);
                }
                if (id != Guid.Empty)
                    result = xBLL.Update<XSHTMXB>(entity, delMXB);
                else
                    result = xBLL.Insert(entity);
            }
            if (result)
            {
                btnSave.Visible = false;
                labResult.Text = "保存成功";
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
            }
            else
                labResult.Text = "保存失败";
        }
    }
}